<?php

/**
 * Permission model
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://monzee.wordpress.com/license New BSD
 * @version   $Id: Permissions.php 38 2009-02-18 17:12:39Z monzee $
 */
class Admin_Permissions
{
    protected $_permissions;

    public function __construct()
    {
        $this->_permissions = array(
            array(
                'resource_id' => 'admin/index/index',
                'role_id'     => 1, // Administrator
            ),
            array(
                'resource_id' => 'default/index/index',
                'role_id'     => 1, // Administrator
            ),
            array(
                'resource_id' => 'default/index/index',
                'role_id'     => 2, // Guest. no need to define for the rest
                                    // since they all inherit Guest's permissions
            ),
            array(
                'resource_id' => 'blog/posts/new',
                'role_id'     => 4, // Author
            ),
            array(
                'resource_id' => 'blog/posts/create',
                'role_id'     => 4, // Author
            ),
            array(
                'resource_id' => 'blog/posts/edit',
                'role_id'     => 4, // Author
            ),
            array(
                'resource_id' => 'blog/posts/update',
                'role_id'     => 4, // Author
            ),
            array(
                'resource_id' => 'blog/posts/delete',
                'role_id'     => 4, // Author
            ),
            array(
                'resource_id' => 'blog/posts/destroy',
                'role_id'     => 4, // Author
            ),
            array(
                'resource_id' => 'blog/comments/new',
                'role_id'     => 3, // Member
            ),
            array(
                'resource_id' => 'blog/comments/create',
                'role_id'     => 3, // Member
            ),
            array(
                'resource_id' => 'blog/comments/new',
                'role_id'     => 3, // Member
            ),
            array(
                'resource_id' => 'blog/comments/edit',
                'role_id'     => 3, // Member
            ),
            array(
                'resource_id' => 'blog/comments/update',
                'role_id'     => 3, // Member
            ),
            array(
                'resource_id' => 'blog/comments/delete',
                'role_id'     => 5, // Moderator
            ),
            array(
                'resource_id' => 'blog/comments/destroy',
                'role_id'     => 5, // Moderator
            ),
        );
    }

    public function setPermissions($permissions)
    {
        $this->_permissions = $permissions;
    }

    /**
     * Returns an array of role ids which are allowed to access a resource
     *
     * @param array $resId
     * @return array
     */
    public function getRolesByResourceId($resId)
    {
        $permissions = $this->_permissions;
        $ret = array();
        foreach ($permissions as $permission) {
            if ($permission['resource_id'] === $resId) {
                $ret[] = $permission['role_id'];
            }
        }
        return $ret;
    }

    public function getAll()
    {
        return $this->_permissions;
    }

}
